//
//  MyTableViewCell.m
//  aa
//
//  Created by IOS_wt on 15/12/1.
//  Copyright © 2015年 wangteng. All rights reserved.
//

#import "MyTableViewCell.h"

@implementation MyTableViewCell
{
    NSArray * constraints1;
    NSLayoutConstraint*constraints1V;
    NSArray *constraints2;
    NSArray *constraints2H;
    NSArray *constraints3;
    NSArray *constraints3V;
    
    
    
}
- (void)awakeFromNib {
    // Initialization code
    self.selectionStyle=UITableViewCellSelectionStyleNone;//cell不可选
    
    //image的
    self.myImage=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cb3.png"]];
    [self.myImage sizeToFit];
    [self addSubview:self.myImage];
    constraints1=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_myImage(80)]"options:0 metrics:nil views:@{@"_myImage":self->_myImage}];
    [self addConstraints:constraints1];
    
    constraints1V=[NSLayoutConstraint constraintWithItem:self->_myImage attribute:   NSLayoutAttributeCenterY relatedBy: NSLayoutRelationEqual toItem:self attribute:    NSLayoutAttributeCenterY multiplier:1 constant:0];
    
    [self addConstraint:constraints1V];
    
    //button的
    self.myButton=[[UIButton alloc]init];
    [self.myButton setTitle:@"ok" forState: UIControlStateNormal];
    self.myButton.titleLabel.font = [UIFont systemFontOfSize: 20.0];
    [self.myButton setTitleColor:[UIColor redColor]forState:UIControlStateNormal];
    self.myButton.backgroundColor=[UIColor greenColor];
    [self.myButton addTarget:self action:@selector(clicked) forControlEvents:UIControlEventTouchDown];
    [self addSubview:self.myButton];
    
    constraints2=[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_myButton(30)]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_myButton)];
    [self addConstraints:constraints2];
    
    constraints2H=[NSLayoutConstraint constraintsWithVisualFormat:@"H:[_myButton(30)]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_myButton)];
    [self addConstraints:constraints2H];
    
    
    //label的
    self.myLable=[[UILabel alloc]init];
    self.myLable.text=@"在cell里面，为每一组特定的约束条件使用一个特定的cell重用标示符。换句话说，如果cell有多种不同的布局，每一种布局应当有其对应的重用标示符。DFSAGFRDSKLFGJQ'LFGJDKPJFKDJGKFJKGJKFRJGGGGGGDSFGRWERYRTUYT";
    self.myLable.lineBreakMode = NSLineBreakByCharWrapping;//换行的
    self.myLable.numberOfLines = 0;
    self.myLable.backgroundColor=[UIColor cyanColor];
    [self.myLable sizeToFit];
    [self addSubview:self.myLable];
    NSDictionary *dicts = NSDictionaryOfVariableBindings(_myImage, _myLable,_myButton);//@{@"myLable":self->_myLable}
    constraints3=[NSLayoutConstraint constraintsWithVisualFormat:@"H:[_myImage]-[_myLable]|" options:0 metrics:nil views:dicts];
    [self addConstraints:constraints3];
    
    constraints3V=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_myLable]-[_myButton]" options:0 metrics:nil views:dicts];
    [self addConstraints:constraints3V];
    
    
    
    self.myImage.translatesAutoresizingMaskIntoConstraints=NO;
    self.myLable.translatesAutoresizingMaskIntoConstraints=NO;
    self.myButton.translatesAutoresizingMaskIntoConstraints=NO;
    
    [self->_myImage layoutIfNeeded];
    [self->_myLable layoutIfNeeded];
    [self->_myButton layoutIfNeeded];
    [super updateConstraints];
    
    
}

-(void)clicked{
    
    [self removeConstraints:constraints1];
    [self removeConstraint:constraints1V];
    [self removeConstraints:constraints3];
    [self removeConstraints:constraints3V];
    
    NSDictionary *dict = NSDictionaryOfVariableBindings(_myImage, _myLable);
    
    //创建动画并指定动画属性
    CABasicAnimation *basicAnimation=[CABasicAnimation animationWithKeyPath:@"position"];
    
    //设置其他动画属性
    basicAnimation.duration=2.0;//动画时间2秒
    basicAnimation.removedOnCompletion=NO;//运行一次是否移除动画
    basicAnimation.delegate=self;
    //添加动画到图层，注意key相当于给动画进行命名，以后获得该图层时可以使用此名称获取
    [self->_myImage.layer addAnimation:basicAnimation forKey:@"KCBasicAnimation_Translation"];
   [self->_myLable.layer addAnimation:basicAnimation forKey:@"KCBasicAnimation_Translation"];
    
    
    NSArray *constraints4=[NSLayoutConstraint constraintsWithVisualFormat:@"H:[_myImage(80)]|"options:0 metrics:nil views:dict];
    [self addConstraints:constraints4];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self->_myImage attribute:  NSLayoutAttributeCenterY relatedBy: NSLayoutRelationEqual toItem:self attribute:   NSLayoutAttributeCenterY multiplier:1 constant:0]];
    
    NSArray *constraints5=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_myLable]-[_myImage]" options:0 metrics:nil views:dict];
    [self addConstraints:constraints5];
    NSArray *constraints5V=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_myLable]" options:0 metrics:nil views:@{@"_myLable":self->_myLable}];
    [self addConstraints:constraints5V];
    
    [self->_myImage layoutIfNeeded];
    [self->_myLable layoutIfNeeded];
    
    
}




- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
